// featurePackage/pages/searchResult/searchResult.js

import { querySearchShopProducts } from "../../../service/jb_home_service"
import debounce from "../../../tools/debounce"
import {
  addGoodsToMyShop
} from "../../../service/jb_smallshop_service"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    layoutType: "one",//两行 one:单行
    goodList: [

    ],

    isBottomShow: false,
    refreshFlag: false,
    isShowLoading: false,
    searchWord: "",
    page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.searchWord) {
      this.setData({
        searchWord: options.searchWord
      })
    }
    this.querySearchShopProducts();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  //搜索变化
  searchBarChange(e) {
    console.log("JJLog:e:" + JSON.stringify(e));
    this.setData({
      searchWord: e.detail
    })
    debounce(this.querySearchShopProducts('refresh'));
  },

  //列表布局切换
  layoutChangeAction() {
    this.setData({
      layoutType: this.data.layoutType == 'one' ? 'two' : 'one'
    })
  },
  querySearchShopProducts(type) {
    wx.showLoading();
    querySearchShopProducts(this.data.searchWord, this.data.page).then(res => {
      wx.hideLoading()
      if (res.code == 200 && res.data) {
        if (res.data.list && res.data.list.length > 0) {
          res.data.list.forEach(i => {
            i.isSelect = false;
          })
        }

        let arr = (type == 'refresh' ? [] : this.data.goodList || []);
        arr = arr.concat(res.data.list || [])
        this.setData({
          goodList: arr,
          refreshFlag: false,
          isShowLoading: false,
          isBottomShow: res.data.totalPage == this.data.page ? true : false,
        })
      } else {
        this.setData({
          goodList: [],
          refreshFlag: false,
          isShowLoading: false,
          isBottomShow: false,
        })
      }
      console.log("JJLog:搜索结果是:" + JSON.stringify(res));
    }).catch(err => {
      wx.hideLoading()
      console.error("JJLog:querySearchShopProducts", err);
      wx.showToast({
        title: err || '加载失败',
        icon: "none"
      })
      if (this.data.page != 1) {
        this.setData({
          page: this.data.page - 1
        })
      }
      this.setData({
        refreshFlag: false,
      })
    })
  },
  //商品状态改变
  goodSelectChange(e) {
    let index = e.detail.index;
    let arr = this.data.goodList;
    arr[index].isSelect = !arr[index].isSelect;
    this.setData({
      goodList: arr
    })
  },
  //确认添加商品：
  sureAddGoods() {
    let selArr = [];
    this.data.goodList.forEach(i => {
      if (i.isSelect) {
        selArr.push(i.productId)
      }
    })
    if (selArr.length < 1) {
      wx.showToast({
        title: '请选择商品',
        icon: "none"
      })
      return;
    }
    wx.showLoading();
    //调用添加接口：
    addGoodsToMyShop({ productIds: selArr }).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '添加成功',
          icon: "success"
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)

      }
    }).catch(err => {
      wx.hideLoading();
      console.error("JJLog:addGoodsToMyShop", err)
      wx.showToast({
        title: err || '加载失败',
        icon: "none"
      })
    })

  },


  // 下拉刷新
  handleRefresh() {
    // TODO
    this.setData({
      page: 1,
      isBottomShow: false,
      refreshFlag: true,
      searchWord: '',
    })
    this.querySearchShopProducts('refresh');
  },
  // 加载更多
  handleLoadmore() {
    console.log('加载更多');
    if (this.data.isBottomShow) {
      return;
    }
    this.setData({
      isShowLoading: true,
      page: this.data.page + 1
    })
    this.querySearchShopProducts()
  },







  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})